//#include "config.h"
#include "stdlib.h"
#include "stdint.h"
#include <string.h>
#include "math.h"

#include "..\..\BSP\bsp_gpio.h"
#include "..\..\BSP\bsp_pwm.h"
#include "..\..\BSP\bsp_uart.h"
#include "..\..\BSP\bsp_nvic.h"
#include "..\..\BSP\bsp_exit.h"
#include "..\..\BSP\bsp_lvr.h"
#include "..\..\BSP\bsp_wdt.h"
#include "..\..\BSP\bsp_timer.h"
#include "..\..\BSP\delay.h"
#include "bsp_Dis2.h"



#define adress 	       0xB0

//#define SDA_OUT()    
static void SDA_OUT()
{
	
	GPIO_InitTypeDef GPIO_InitStructure;
	
	GPIO_PinAFConfig(GPIOB, GPIO_Pin_7 , PB7_GPIO);
	GPIO_WriteBit(GPIOB, GPIO_Pin_7, Bit_SET);
	GPIO_StructInit(&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_ODrv = GPIO_ODrv_NORMAL;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
}


//sda线输出
//#define SDA_IN()                 //sda线输出

static void SDA_IN()
{
	
	GPIO_InitTypeDef GPIO_InitStructure;
	
	GPIO_PinAFConfig(GPIOB, GPIO_Pin_7 , PB7_GPIO);
	GPIO_WriteBit(GPIOB, GPIO_Pin_7, Bit_SET);
	GPIO_StructInit(&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
    GPIO_InitStructure.GPIO_ODrv = GPIO_ODrv_NORMAL;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
	//GPIO_OType_PP
}

#define IIC_SCL_SET    	GPIO_WriteBit(GPIOB, GPIO_Pin_6, Bit_SET)		// SCL IO高电平  // SCL IO
#define IIC_SCL_RESET   GPIO_WriteBit(GPIOB, GPIO_Pin_6, Bit_RESET)	// SCL IO低电平   // SCL IO
#define IIC_SDA_SET     GPIO_WriteBit(GPIOB, GPIO_Pin_7, Bit_SET)		// SDA IO高电平  // SDA IO
#define IIC_SDA_RESET   GPIO_WriteBit(GPIOB, GPIO_Pin_7, Bit_RESET)	// SDA IO低电平   // SDA IO

#define IIC_READ_SDA 	LL_GPIO_IsInputPinSet()


static uint8_t Touch_Value1;


static uint8_t  LL_GPIO_IsInputPinSet(void)
{
	if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7) == 1) 
	{
		return 1;
	}
	else 
	{
		return 0;
	}
} 


 void Dis2_iic_init(void)
{
 IIC_SCL_SET    ;

 IIC_SDA_SET  ;
	
}

static void my_iic_start(void)
{
    SDA_OUT();     //sda线输出
    IIC_SDA_SET;
    IIC_SCL_SET;
   rt_hw_us_delay(20);
    IIC_SDA_RESET;//START:when CLK is high,DATA change form high to low
   rt_hw_us_delay(20);
    IIC_SCL_RESET;//钳住I2C总线，准备发送或接收数据
}

static void my_iic_stop(void)
{
    SDA_OUT();//sda线输出
    IIC_SCL_RESET;
    IIC_SDA_RESET;//STOP:when CLK is high DATA change form low to high
   rt_hw_us_delay(20);
    IIC_SCL_SET;
   rt_hw_us_delay(20);
    IIC_SDA_SET;//发送I2C总线结束信号
}

static uint8_t my_iic_wait_ack(void)
{
    uint8_t ucErrTime = 0;
    SDA_IN();      //SDA设置为输入
    IIC_SDA_SET;rt_hw_us_delay(20);
    IIC_SCL_SET;rt_hw_us_delay(20);
    while (IIC_READ_SDA)
    {
        ucErrTime++;
				rt_hw_us_delay(20);;
        if (ucErrTime > 250)
        {
            my_iic_stop();
            return 1;
        }
    }
    IIC_SCL_RESET;//时钟输出0
    return 0;
}

static void my_iic_ack(void)
{
    IIC_SCL_RESET;
    SDA_OUT();
    IIC_SDA_RESET;
   rt_hw_us_delay(20);
    IIC_SCL_SET;
   rt_hw_us_delay(20);
    IIC_SCL_RESET;
}
//不产生ACK应答
static void my_iic_nack(void)
{
    IIC_SCL_RESET;
    SDA_OUT();
    IIC_SDA_SET;
   rt_hw_us_delay(20);
    IIC_SCL_SET;
   rt_hw_us_delay(20);
    IIC_SCL_RESET;
}


//IIC发送一个字节
//返回从机有无应答
//1，有应答
//0，无应答
static void my_iic_send_byte(uint8_t txd)
{
    uint8_t t;
    SDA_OUT();
    IIC_SCL_RESET;//拉低时钟开始数据传输
    for (t = 0; t < 8; t++)
    {
        if ((txd & 0x80) >> 7)
        {
            IIC_SDA_SET;
        }
        else
            IIC_SDA_RESET;
        txd <<= 1;
       rt_hw_us_delay(20);   //对TEA5767这三个延时都是必须的
        IIC_SCL_SET;
       rt_hw_us_delay(20);
        IIC_SCL_RESET;
        //rt_hw_us_delay(20);
    }
}
//读1个字节，ack=1时，发送ACK，ack=0，发送nACK
static uint8_t my_iic_read_byte(uint8_t ack)
{
    unsigned char i, receive = 0;
	
	IIC_SDA_SET;  //ARM内核
    SDA_IN();//SDA设置为输入
	rt_hw_us_delay(20);
    for (i = 0; i < 8; i++ )
    {
        IIC_SCL_RESET;
       rt_hw_us_delay(20);
        IIC_SCL_SET;
        receive <<= 1;
        if (IIC_READ_SDA)receive++;
       rt_hw_us_delay(20);
    }
    if (!ack)
        my_iic_nack();//发送nACK
    else
        my_iic_ack(); //发送ACK
    return receive;
}



/***********************************左显示板代码**********************************************/

static void My_read_bytes(uint8_t DATA[], uint8_t num)
{
	int i = 0;
	my_iic_start();

	my_iic_send_byte(adress|0x01);
	my_iic_wait_ack();

	
//	my_iic_send_byte(addr |0x04 ); //发送低地址
//	my_iic_wait_ack();
//	my_iic_start();
//	my_iic_send_byte(0xa1); //进入接收模式
//	my_iic_wait_ack();
	for (i = 0; i < num - 1; i++)
	{
		DATA[i] = my_iic_read_byte(1);
	}
	DATA[i] = my_iic_read_byte(0);
	my_iic_stop();
}


static void My_write_bytes(uint8_t DATA[], uint8_t num)
{
	uint8_t i =0;
	
	my_iic_start();

	my_iic_send_byte(adress);
	
	my_iic_wait_ack();
	
//	my_iic_send_byte(addr); 
//	
//	my_iic_wait_ack();
	
	for (i = 0; i < num; i++)
	{
		my_iic_send_byte(*(DATA + i)); //发送字节
		my_iic_wait_ack();
	}
	my_iic_stop(); //产生一个停止条件

}


void  Read_Dis2_KEY()
{
	uint8_t data_1[4] ={0};
	uint8_t i;
	uint8_t u8TwiTxCheSum =0;
	
	My_read_bytes(data_1, 4);
	
	for(i =0;i<3;i++)
	{
		u8TwiTxCheSum = data_1[i] +u8TwiTxCheSum;
	}
	u8TwiTxCheSum =0xff^u8TwiTxCheSum;
	if(u8TwiTxCheSum ==data_1[3])
	{
		Touch_Value1 =data_1[0];
	}
}


uint8_t  Wirte_Dis2_KEY(uint8_t* Buff)
{
	static	uint8_t Date[4] ={0};
	uint8_t i;
	uint8_t u8TwiTxCheSum=0;

	if(memcmp(&Buff, &Date, 3) != 0)
	{
		Date[0] =(Buff[0]);
		Date[1] =(Buff[1]);
		Date[2] =(Buff[2]);
		
		for(i =0;i<3;i++)
		{
			u8TwiTxCheSum = Date[i] +u8TwiTxCheSum;
		}
		u8TwiTxCheSum =0xff^u8TwiTxCheSum;
		Date[3] =u8TwiTxCheSum;

		My_write_bytes(Date, 4);
	}
	return 1;
}


uint8_t  Read_Dis2_Value1(void)
{
	return Touch_Value1;
}

